package com.kangye.exam.controller;

import com.fasterxml.jackson.core.json.async.NonBlockingJsonParser;
import com.kangye.exam.bean.Subject;
import com.kangye.exam.service.SubjectService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author 海风
 * 对学科表的增删改查操作
 * @data 2021-3-15 18:47:18
 * @since 1.1.5
 * */
@RestController
@RequestMapping("/subject")
@CrossOrigin
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    /*
     * 查询所有学科
     *
     * */
    @RequestMapping("/find_all")
    public Object find_all() {
        List<Subject> list = subjectService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", list);
        return map;
    }

    /*
     * 通过id查找学科
     * @id 学科id
     * */
    @RequestMapping("/find/{id}")
    public Object find_id(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Subject subject = subjectService.selectByID(id);
        map.put("code", 0);
        map.put("data", subject);
        return map;
    }

    /*
     * 插入一条学科
     * @subject 学科class
     * */
    @RequestMapping("/insert")
    public Object insert(Subject subject) {

        return subjectService.insert(subject) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 修改一条学科
     * @subject 学科class,判空
     * */
    @RequestMapping("/update")
    public Object update(Subject subject) {

        return subjectService.update(subject) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 删除一条学科
     * @id 需要删除的学科id
     * */
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {

        return subjectService.delete(id) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }
}
